package com.lhy.framework.context.support;

import com.lhy.framework.beans.BeansException;
import com.lhy.framework.beans.factory.ConfigurablelistableBeanFactory;
import com.lhy.framework.beans.factory.factory.BeanFactoryPostProcessor;
import com.lhy.framework.beans.factory.factory.BeanPostProcessor;
import com.lhy.framework.context.ConfigurableApplicationContext;
import com.lhy.framework.core.io.DefaultResourceLoader;

import java.util.List;
import java.util.Map;

/**
 * @ClassName AbstractApplicationContext
 * @Description
 * @Author lihaiyang
 * @Date 2022/9/11 11:42
 * @Version 1.0
 */
public abstract class AbstractApplicationContext extends DefaultResourceLoader implements ConfigurableApplicationContext {

    @Override
    public void refresh() throws BeansException {

        // 1、创建BeanFactory，并加载BeanDefinition
        refreshBeanFactory();

        // 2、获取BeanFactory
        ConfigurablelistableBeanFactory beanFactory = getBeanFactory();

        // 3、添加ApplicationContextAwareProcessor，让继承自ApplicationContextAware的Bean对象都能够感知所属ApplicationContext
        beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));

        // 4、在Bean实例化之前，执行BeanFactoryPostProcessor （Invoke factory processors registered as beans in the context）
        invokeBeanFactoryPostProcessors(beanFactory);

        // 5、BeanPostProcessor 需要提前于其他Bean对象实例化之前执行注册操作
        registerBeanPostProcessors(beanFactory);

        // 6、提前实例化单例Bean对象
        beanFactory.preInstantiateSingletons();
    }

    /**
     * 创建BeanFactory，并加载BeanDefinition
     */
    protected abstract void refreshBeanFactory() throws BeansException;

    /**
     * 获取BeanFactory
     * @return
     * @throws BeansException
     */
    protected abstract ConfigurablelistableBeanFactory getBeanFactory() throws BeansException;

    /**
     * 在Bean实例之前，执行BeanFactoryProcessor
     * @param beanFactory
     */
    private void invokeBeanFactoryPostProcessors(ConfigurablelistableBeanFactory beanFactory) {
        Map<String, BeanFactoryPostProcessor> beanFactoryPostProcessorMap = beanFactory.getBeansOfType(BeanFactoryPostProcessor.class);
        for (BeanFactoryPostProcessor beanFactoryPostProcessor : beanFactoryPostProcessorMap.values()) {
            beanFactoryPostProcessor.postProcessBeanFactory(beanFactory);
        }
    }

    /**
     * 注册BeanPostProcessors
     * @param beanFactory
     */
    private void registerBeanPostProcessors(ConfigurablelistableBeanFactory beanFactory) {
        Map<String, BeanPostProcessor> beanPostProcessorMap = beanFactory.getBeansOfType(BeanPostProcessor.class);
        for (BeanPostProcessor beanPostProcessor : beanPostProcessorMap.values()) {
            beanFactory.addBeanPostProcessor(beanPostProcessor);
        }
    }

    @Override
    public <T> Map<String, T> getBeansOfType(Class<T> type) {
        return getBeanFactory().getBeansOfType(type);
    }

    @Override
    public List<String> getBeanDefinitionNames() {
        return getBeanFactory().getBeanDefinitionNames();
    }

    @Override
    public Object getBean(String beanName) throws BeansException {
        return getBeanFactory().getBean(beanName);
    }

    @Override
    public Object getBean(String beanName, Object... args) throws BeansException {
        return getBeanFactory().getBean(beanName, args);
    }

    @Override
    public <T> T getBean(String beanName, Class<T> requiredType) throws BeansException {
        return getBeanFactory().getBean(beanName, requiredType);
    }

    @Override
    public void registerShutdownHook() {
        Runtime.getRuntime().addShutdownHook(new Thread(this::close));
    }

    @Override
    public void close() {
        getBeanFactory().destroySingletons();
    }
}
